/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.spring.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.security.bean.Permission;
import com.genia.toolbox.persistence.criteria.Criteria;
import com.genia.toolbox.persistence.criteria.CriteriaType;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Role;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.RoleDAO;

/**
 * implementation of {@link RoleDAO}.
 */
public class RoleDAOImpl
    extends AbstractDAO
    implements RoleDAO
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(RoleDAOImpl.class);



  /**
   * returns the {@link List} of {@link Role} having the given permission.
   * 
   * @param permission
   *          the permission to check for
   * @return the {@link List} of {@link Role} having the given permission
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.RoleDAO#getRolesForPermission(com.genia.toolbox.projects.toolbox_basics_project.bean.PermissionDescription)
   */
  @Transactional(readOnly = true)
  public List<Role> getRolesForPermission(Permission permission)
      throws PersistenceException
  {
    if (permission == null) {
      LOGGER.warn("Asking role for null permission. Check your definitions.");
      return new ArrayList<Role>();
    }
    Criteria<Role> criteria = getCriteria(Role.class);
    criteria.setDistinct(true);
    CriteriaType<String> permissions = criteria.join(Role.NAME_PERMISSIONS);
    criteria.addRestriction(equals(getConstantField(permission.getAuthority()), getObjectField(permissions)));
    return simpleFind(criteria.project(criteria.getMainType()));
  }



  /**
   * returns all the {@link Role}s.
   * 
   * @return all the {@link Role}s
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.RoleDAO#getAll()
   */
  @Transactional(readOnly = true)
  public List<Role> getAll()
      throws PersistenceException
  {
    return simpleFind(getCriteria(Role.class));
  }

}
